package com.hj.dapp.business.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.dapp.business.customer.mapper.IntendedCustomerMapper;
import com.hj.dapp.business.customer.model.IntendedCustomer;
import com.hj.dapp.business.customer.service.IntendedCustomerService;
import com.hj.dapp.business.engineer.model.EngineerProject;
import com.hj.dapp.business.engineer.service.EngineerProjectService;
import com.hj.dapp.business.user.model.User;
import com.hj.dapp.business.user.service.UserService;
import com.hj.dapp.common.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

import static com.hj.dapp.common.constant.DictEnum.DELETE_FLAG;
import static com.hj.dapp.common.constant.DictEnum.NORMAL_FLAG;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/22
 */
@Service
public class IntendedCustomerServiceImpl implements IntendedCustomerService {
    @Autowired
    private IntendedCustomerMapper intendedCustomerMapper;
    @Autowired
    private EngineerProjectService engineerProjectService;
    @Autowired
    private UserService userService;

    @Override
    public void save(IntendedCustomer intendedCustomer, Principal principal) {
        if (ObjectUtils.isEmpty(intendedCustomer.getHeadId())) {
            User user = userService.getUserByUserName(principal.getName());
            intendedCustomer.setHeadId(user.getId());
        }
        intendedCustomer.setCreateBy(principal.getName());
        intendedCustomer.setCreateTime(LocalDateTime.now());
        intendedCustomer.setDelFlag(NORMAL_FLAG.getValue());

        intendedCustomerMapper.insert(intendedCustomer);
        intendedCustomer.getEngineerProjects().forEach(n -> {
            n.setIntendedCustomerId(intendedCustomer.getId());
            engineerProjectService.save(n);
        });
    }

    @Override
    public IntendedCustomer get(Long id) {
        IntendedCustomer intendedCustomer = intendedCustomerMapper.selectById(id);
        List<EngineerProject> engineerProjects = engineerProjectService.listByIntendedCustomer(id);
        intendedCustomer.setEngineerProjects(engineerProjects);
        return intendedCustomer;
    }

    @Override
    public IPage<IntendedCustomer> page(PageRequest<IntendedCustomer> request, Principal principal) {
        Page<IntendedCustomer> page = new Page<>();
        page.setSize(request.getSize());
        page.setCurrent(request.getCurrent());
        IntendedCustomer query = request.getQuery();
        query.setCreateBy(principal.getName());
        IPage<IntendedCustomer> pageVO = intendedCustomerMapper.selectPageVO(page, query);
        pageVO.getRecords().forEach(n -> {
            List<EngineerProject> engineerProjects = engineerProjectService.listByIntendedCustomer(n.getId());
            n.setEngineerProjects(engineerProjects);
        });
        return pageVO;
    }

    @Override
    public void delete(Long id) {
        IntendedCustomer intendedCustomer = intendedCustomerMapper.selectById(id);
        intendedCustomer.setDelFlag(DELETE_FLAG.getValue());
        intendedCustomerMapper.updateById(intendedCustomer);
    }

    @Override
    public void edit(IntendedCustomer intendedCustomer, Principal principal) {
        intendedCustomer.setLastUpdateBy(principal.getName());
        intendedCustomer.setLastUpdateTime(LocalDateTime.now());
        intendedCustomerMapper.updateById(intendedCustomer);
        engineerProjectService.deleteIntendedCustomerProject(intendedCustomer.getId());
        intendedCustomer.getEngineerProjects().forEach(n->{
            n.setIntendedCustomerId(intendedCustomer.getId());
            engineerProjectService.save(n);
        });
    }

    @Override
    public List<IntendedCustomer> list(String query, Principal principal) {
        User user = userService.getUserByUserName(principal.getName());
        QueryWrapper<IntendedCustomer> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(query)) {
            queryWrapper.like("name", query);
        }
        queryWrapper.and(qr ->
                qr.eq("head_id", user.getId())
                        .or().eq("create_by",principal.getName())
        );
        queryWrapper.eq("del_flag", NORMAL_FLAG);
        List<IntendedCustomer> intendedCustomers = intendedCustomerMapper.selectList(queryWrapper);
        intendedCustomers.forEach(n -> {
            List<EngineerProject> engineerProjects = engineerProjectService.listByIntendedCustomer(n.getId());
            n.setEngineerProjects(engineerProjects);
        });
        return intendedCustomerMapper.selectList(queryWrapper);
    }
}
